﻿
namespace BackupLib
{
    using System.Xml;
    using System.Xml.Serialization;
    using System;
    using System.Text;

    public class XML
    {

        public static void Serialize(BackupProject obj)
        {
            Serialize(obj, Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Backup\\");
        }

        public static void Serialize(BackupProject obj, string configPath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            APIWrapper.CreateDirectory(configPath);
            XmlTextWriter writer = new XmlTextWriter(configPath + "backup.xml", Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            serializer.Serialize(writer, obj);
            writer.Close();
        }

        public static BackupProject Deserialize()
        {
             return Deserialize( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\Backup\\");
       }

        public static BackupProject Deserialize(string configPath)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProject));
            XmlTextReader reader = new XmlTextReader(configPath + "backup.xml");
            BackupProject retval;
            try
            {
                retval = serializer.Deserialize(reader) as BackupProject;
            }
            catch (Exception)
            {
                retval = new BackupProject();
            }
            reader.Close();
            return retval;
        }
    }
}
